﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utils;
using System.Text;
using System.Web.Security;
using System.Data;
using Maticsoft.DBUtility;
using System.Collections.Specialized;

public partial class account_signup : FBasePage
{
    // Fields


    // Methods
    protected void btnRist_Click(object sender, EventArgs e)
    {
        string s = Helper.GetString(base.Request.Form["email"], string.Empty);
        string str2 = Helper.GetString(base.Request.Form["username"], string.Empty);
        string str3 = base.Request.Form["password"].ToString().Trim();
        string str4 = base.Request.Form["passwordconfirm"].ToString().Trim();
        string str5 = base.Request.Form["mobile"].ToString().Trim();
        int @int = Helper.GetInt(base.Request.Form["city"], 0);
        if (Helper.GetString(base.Request.Form["username"], "login") == "login")
        {
            this.SetError("您输入的用户名不符合规范！");
            base.Response.Redirect("signup.aspx");
        }
        if (!str3.Equals(str4))
        {
            this.SetError("密码不一致！");
            base.Response.Redirect("signup.aspx");
            base.Response.End();
        }
        else if (str2 == "")
        {
            this.SetError("用户名不能为空！");
            base.Response.Redirect("signup.aspx");
            base.Response.End();
        }
        else
        {
            int byteCount = Encoding.Default.GetByteCount(str2);
            if ((byteCount > 0x10) || (byteCount < 4))
            {
                this.SetError("用户名长度为4-16个字符，一个汉字为两个字符！");
                base.Response.Redirect("signup.aspx");
                base.Response.End();
            }
            else if (str3 == "")
            {
                this.SetError("密码不能为空！");
                base.Response.Redirect("signup.aspx");
                base.Response.End();
            }
            else
            {
                Maticsoft.BLL.UserInfo info = new Maticsoft.BLL.UserInfo();
                Maticsoft.Model.UserInfo model = new Maticsoft.Model.UserInfo();
                model.Username = base.Server.HtmlEncode(str2);
                model.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(str3 + "@4!@#$%@", "md5");
                model.Mobile = base.Server.HtmlEncode(str5);
                model.Email = base.Server.HtmlEncode(s);
                model.City_id = @int;
                model.Gender = "M";
                model.Create_time = DateTime.Now;
                model.IP = this.Page.Request.UserHostAddress;
                model.Newbie = "N";
                model.Avatar = "/upfile/img/user-no-avatar.gif";
                string strWhere = "";
                strWhere = string.Format(" email='{0}' or username='{1}'", s, str2);
                DataSet list = info.GetList(strWhere);
                if (((list != null) && (list.Tables.Count > 0)) && (list.Tables[0].Rows.Count > 0))
                {
                    this.SetError("已有该账户");
                    base.Response.Redirect("signup.aspx");
                    base.Response.End();
                }
                else
                {
                    DataSet set2 = info.GetList("");
                    if (int.Parse(info.GetCount("count(*)", "")) == 0)
                    {
                        model.Manager = "Y";
                    }
                    bool flag = true;
                    string recode = "";
                    model.IP_Address = CookieHelper.GetCookieValue("gourl");
                    if (base.ASSystem != null)
                    {
                        if (base.ASSystem.Emailverify == 1)
                        {
                            recode = Helper.GetRandomString(0x20);
                            model.Enable = "N";
                            model.Recode = recode;
                            flag = false;
                            base.SetSuccess("注册成功,请到邮箱激活");
                        }
                        else
                        {
                            model.Enable = "Y";
                            flag = true;
                            base.SetSuccess("注册成功");
                        }
                    }
                    int num3 = 0;
                    num3 = info.Add(model);
                    if (Helper.GetInt(base.Request["subscribe"], 0) == 1)
                    {
                        Maticsoft.BLL.Mailer mailer = new Maticsoft.BLL.Mailer();
                        if (mailer.GetModelList(string.Concat(new object[] { "email='", s, "' and city_id=", @int })).Count == 0)
                        {
                            Maticsoft.Model.Mailer mailer2 = new Maticsoft.Model.Mailer();
                            mailer2.City_id = @int;
                            mailer2.Email = base.Server.HtmlEncode(s);
                            mailer2.Secret = Helper.GetRandomString(0x20);
                            mailer.Add(mailer2);
                        }
                    }
                    if (num3 > 0)
                    {
                        int maxId = info.GetMaxId();
                        if ((this.Session["invitor"] != null) && (this.Session["invitor"].ToString() != ""))
                        {
                            Maticsoft.BLL.Invite invite = new Maticsoft.BLL.Invite();
                            Maticsoft.Model.Invite invite2 = new Maticsoft.Model.Invite();
                            invite2.Create_time = DateTime.Now;
                            invite2.User_id = int.Parse(this.Session["invitor"].ToString());
                            invite2.Other_user_id = maxId;
                            invite2.Other_user_ip = base.Request.UserHostAddress;
                            invite2.Pay = "N";
                            invite.Add(invite2);
                            this.Session["invitor"] = null;
                        }
                        if (flag)
                        {
                            FormsAuthentication.SetAuthCookie(str2, false);
                            base.Response.Redirect("~/index.aspx");
                        }
                        else
                        {
                            this.Session["useremail"] = s;
                            this.sentEmail(s, str2, recode);
                            base.Response.Redirect("signuped.aspx");
                        }
                    }
                }
            }
        }
    }

    private void initPage()
    {
        DataTable cityList = base.CityList;
        if ((cityList != null) && (cityList.Rows.Count > 0))
        {
            for (int i = 0; i < cityList.Rows.Count; i++)
            {
                this.city.Items.Add(new ListItem(cityList.Rows[i]["Name"].ToString(), cityList.Rows[i]["id"].ToString()));
            }
        }
        this.city.Items.Add(new ListItem("全部城市", "0"));
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            this.initPage();
            base.SetTitle("注册");
        }
    }

    private void sentEmail(string strEmail, string strUserName, string Recode)
    {
        string str = "";
        string str2 = "";
        str2 = base.Request.Url.AbsoluteUri.Substring(7);
        str2 = "http://" + str2.Substring(0, str2.IndexOf('/'));
        NameValueCollection values = new NameValueCollection();
        values.Add("username", strUserName);
        values.Add("sitename", str);
        values.Add("wwwprefix", str2);
        values.Add("recode", Recode);
        string str3 = WebSiteHelper.LoadTemplate("~/template/mail_verify.html", values);
        string subject = "感谢注册" + base.ASSystem.sitename + "，请验证Email以获取更多服务";
        string body = str3;
        List<string> toMails = new List<string>();
        toMails.Add(strEmail);
        string error = string.Empty;
        if (base.SendMail(toMails, subject, body, out error))
        {
            base.SetSuccess("邮件发送成功！");
            base.Response.Redirect("signuped.aspx");
        }
        else
        {
            this.SetError("邮件发送失败！请检查SMTP设置");
            base.Response.Redirect("signuped.aspx");
        }
    }

}